Ext.namespace('Ext.Tasks');
Ext.Tasks.flow_at = [['1', '分公司级别审核'], ['2', '大区级别审核'], ['3', '集团级别审核']];
statusStore_new = new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.Tasks.flow_at
						});		
				        
//增加任务start------------------------------------------------

//var newassignmentForm;
//var empsalaryGridPanel;
//var assignmentWin;
//分公司开始--------------------------

var flow_id=new Ext.form.Hidden({
		                    name: 'flow_id',
		                    id:'flow_id'
		                 })
var step_id=new Ext.form.Hidden({
		                    name: 'step_id',
		                    id:'step_id'
		                 })
var flow_insid=new Ext.form.Hidden({
		                    name: 'flow_insid',
		                    id:'flow_insid'
		                 })
var company_code=new Ext.form.Hidden({
		                    name: 'company_code',
		                    id:'company_code'
		                 })
areaassignmentForm = new Ext.form.FormPanel({
				frame : true,
				region:'north',
				id : 'areaassignmentForm',
				autoHeight:true,
				bodyStyle : 'padding:5px 5px 0',
				items : [{

						layout : 'column',
						items : [{
									layout : "form",
									columnWidth : .5,
									items : [flow_id, 
											 step_id,
											 flow_insid,
											 company_code, 
											{
												fieldLabel : '期间',
												xtype : 'textfield',
												name : 'priod',
												anchor : '90%',
												id : 'priod',
												readOnly:true
											}, {
												fieldLabel : '所属机构',
												xtype : 'textfield',
												width : 200,
												anchor : '90%',
												value : session_companyName,
												readOnly : true
											}]
								}, {
									layout : "form",
									columnWidth : .5,
									items : [{
												fieldLabel : '状态',
												xtype : 'combo',
												store : statusStore_new,
												valueField : "key",
												displayField : "value",
												hideName : 'flow_at',
												id : 'flow_at',
												anchor : '90%',
												mode : 'local',
												emptyText : '请选择状态',
												triggerAction : 'all',
												editable : false,
												disabled : true
											},{
												fieldLabel : '下辖机构',
												xtype : 'textfield',
												width : 200,
												id : 'this_company',
												anchor : '90%',
												readOnly : true
											}]
								}]

					}, {
						layout : 'column',
						items : [{
									layout : "form",
									columnWidth : .5,
									items : [{
												xtype : 'textarea',
												fieldLabel : '以往审核意见',
												name : 'flow_memos',
												id : 'flow_memos',
												anchor : '90%',
												readOnly : true
											}]
								}, {
									layout : "form",
									columnWidth : .5,
									items : [{
												xtype : 'textarea',
												fieldLabel : '本步审核意见',
												name : 'flow_memo',
												id : 'flow_memo',
												anchor : '90%',
												emptyText : ''
											}]
								}]
					}]
			});
			
		

//增加任务end------------------------------------------------		

//显示员工薪资start----------------------------
//薪资store 字段暂时不清楚 ？？？？
function contract_type(val){
    if(val=='1'){
        return '劳动合同';
    }else if(val == '2'){
        return '劳务合同';
    }
    return val;
}				
function flow_at(val){
    if(val=='1'){
        return '分公司级别审核';
    }else if(val == '2'){
        return '大区级别审核';
    }else if(val == '3'){
        return '集团级别审核';
    }
    return val;
}	
var agridSm = new Ext.grid.CheckboxSelectionModel();				
agridCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),agridSm,{
			header : '所属机构标识',
			dataIndex : 'company_code',
			width : 80
		}, {
			header : '所属机构',
			dataIndex : 'company_chinese_code',
			width : 70
		}, {
			header : '合同类型',
			dataIndex : 'contract_type',
			width : 150,
			renderer:contract_type
		}, {
			header : '代人数',
			dataIndex : 'ecount',
			width : 60
		}, {
			header : '应发工资',
			dataIndex : 'spay',
			width : 70
		}, {
			header : '实发工资',
			dataIndex : 'epay',
			width : 70
		}, {
			header : '流程状态',
			dataIndex : 'flow_at',
			width : 100,
			renderer:flow_at
		}]);

// grid的数据
aconditionStore = new Ext.data.Store({
			url : 'workflowTaskAction.action',
			reader : new Ext.data.JsonReader({
						root : 'resultL'
					}, [{
								name : 'contract_type',
								mapping : 'contract_type'
							}, {
								name : 'company_code',
								mapping : 'company_code'
							}, {
								name : 'company_chinese_code',
								mapping : 'company_chinese_code'
							}, {
								name : 'ecount',
								mapping : 'ecount'
							}, {
								name : 'spay',
								mapping : 'spay'
							}, {
								name : 'epay',
								mapping : 'epay'
							}, {
								name : 'flow_at',
								mapping : 'flow_at'
							}])
		});
aconditionStore.reload();
var agrid = new Ext.grid.GridPanel({
				id : "AreaPayClass",
				sm:agridSm,
				cm : agridCm,
				region : 'center',
				autoScroll:true,
				store : aconditionStore,
				height: 200,
				width : 700,
				loadMask : true,
				loadMask : {
					msg : '正在加载数据...'
				},
				frame : true
	});
agrid.addListener('rowdblclick', arowdblclickFn);
function arowdblclickFn(grid, rowIndex, e) {
	record=grid.getStore().getAt(rowIndex).data;
	Ext.getCmp('acompanyPayClass').getStore().on('beforeload', function() {
		Ext.getCmp('acompanyPayClass').getStore().baseParams = {
			'actionType' : 0,
			'flag' : 5,
			'filter.company_code' : record.company_code,
			'filter.priod' : Ext.getCmp('areaassignmentForm').findById('priod').getValue()
		}
	});
	Ext.getCmp('acompanyPayClass').getStore().reload();
	acompanyPayClassWin.show();
}
//------------------------------------------------------------------------------------------------------------
acgridCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
			header : '标识',
			dataIndex : 'pay_class_id',
			width : 80
		}, {
			header : '客户标识',
			dataIndex : 'company_code',
			width : 70
		}, {
			header : '薪资类别名称',
			dataIndex : 'pay_class_name',
			width : 80
		}, {
			header : '代缴个税',
			dataIndex : 'tax_setting',
			width : 60
		}, {
			header : '工资发放日',
			dataIndex : 'payday',
			width : 70
		}, {
			header : '是否拉社保',
			dataIndex : 'if_si',
			width : 90
		}, {
			header : '社保对应月',
			dataIndex : 'si_shift',
			width : 80
		}, {
			header : '最低工资',
			dataIndex : 'pay_min',
			width : 80
		}]);

// grid的数据
acconditionStore = new Ext.data.Store({
			url : 'workflowTaskAction.action',
			reader : new Ext.data.JsonReader({
						root : 'resultL',
						id : 'pay_class_id'
					}, [{
								name : 'pay_class_id',
								mapping : 'pay_class_id'
							}, {
								name : 'company_code',
								mapping : 'company_code'
							}, {
								name : 'pay_class_name',
								mapping : 'pay_class_name'
							}, {
								name : 'is_valid',
								mapping : 'is_valid'
							}, {
								name : 'tax_setting',
								mapping : 'tax_setting'
							}, {
								name : 'payday',
								mapping : 'payday'
							}, {
								name : 'if_calculate',
								mapping : 'if_calculate'
							}, {
								name : 'if_si',
								mapping : 'if_si'
							}, {
								name : 'datanode',
								mapping : 'datanode'
							}, {
								name : 'si_shift',
								mapping : 'si_shift'
							}, {
								name : 'tax_code',
								mapping : 'tax_code'
							}, {
								name : 'lastuse_date',
								mapping : 'lastuse_date'
							}, {
								name : 'pay_min',
								mapping : 'pay_min'
							}])
		});
acconditionStore.reload();
var acgrid = new Ext.grid.GridPanel({
				id : "acompanyPayClass",
				cm : acgridCm,
				region : 'center',
				autoScroll:true,
				store : acconditionStore,
				height: 200,
				width : 700,
				loadMask : true,
				loadMask : {
					msg : '正在加载数据...'
				},
				frame : true
	});
acgrid.addListener('rowdblclick', crowdblclickFn);
function crowdblclickFn(grid, rowIndex, e) {
	calculateArea(grid.store.getAt(rowIndex).get('pay_class_id'));
}
var acompanyPayClassWin = new Ext.Window({
		closable : true,
		closeAction:'hide',
		draggable : true,
		id:'acompanyPayClassWin',
		layout:'fit',
		title : '薪资类别',
		width : 600,
		height : 300,
		plain : true,
		items : [acgrid]
	});
//-----------------------------------------------------------------------------------------------------------
function calculateArea(payClassId) {
	//获取表头信息及所对应字段信息---------------------------------------------------------------------------------
	var columnModelResponseText;
	var storeFieldResponseText;
	
	var conn = Ext.lib.Ajax.getConnectionObject().conn;
	conn.open("POST", 'dynamicColumnAction.action',false);
	conn.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
	conn.send("payClassId="+payClassId+"&queryType=columnModel");
	var responseTextVale = Ext.util.JSON.decode(conn.responseText);
	columnModelResponseText = Ext.encode(responseTextVale.columnModelList);
	columnModelResponseText = columnModelResponseText.substr(1, columnModelResponseText.length - 2);
	var reg_header = new RegExp("\"header\"", "g");
	var reg_dataIndex = new RegExp("\"dataIndex\"", "g");
	var reg = new RegExp("\"", "g");
	columnModelResponseText = columnModelResponseText.replace(reg_header, "header");
	columnModelResponseText = columnModelResponseText.replace(reg_dataIndex, "dataIndex");
	columnModelResponseText = columnModelResponseText.replace(reg, "'");
	
	conn.open("POST", 'dynamicColumnAction.action',false);
	conn.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
	conn.send("payClassId="+payClassId+"&queryType=storeField");
	var responseTextVale = Ext.util.JSON.decode(conn.responseText);
	storeFieldResponseText = Ext.encode(responseTextVale.storeFieldList);
	storeFieldResponseText = storeFieldResponseText.substr(1, storeFieldResponseText.length - 2);
	var reg_name = new RegExp("\"name\"", "g");
	var reg_mapping = new RegExp("\"mapping\"", "g");
	storeFieldResponseText = storeFieldResponseText.replace(reg_name, "name");
	storeFieldResponseText = storeFieldResponseText.replace(reg_mapping, "mapping");
	storeFieldResponseText = storeFieldResponseText.replace(reg, "'");

	// 查询显示的表格
	// var sm = new Ext.grid.CheckboxSelectionModel();
	var cm =  new Ext.grid.ColumnModel(eval("[new Ext.grid.RowNumberer(), " 
		+ "{header:'',dataIndex:'payId',hidden:true},"
		+ "{header:'',dataIndex:'his_id',hidden:true},"
		+ "{header:'',dataIndex:'employee_id',hidden:true},"
		+ "{header:'员工姓名',dataIndex:'name'},"
		+ "{header:'工资月份',dataIndex:'period'},"
		+ columnModelResponseText+"]"));
	
	// 数据源
	var paymentCalculateStore = new Ext.data.Store({
		url : 'payEmpdataAction.action',
		reader : new Ext.data.JsonReader({
				root : 'payDataList',
				id: 'paymentCalculateStore'
			}, eval("["
				+ "{name:'payId',mapping:'payId'},"
				+ "{name:'his_id',mapping:'his_id'},"
				+ "{name:'employee_id',mapping:'employee_id'},"
				+ "{name:'name',mapping:'name'},"
				+ "{name:'period',mapping:'period'},"
				+ storeFieldResponseText+"]"))
	});
	var paymentCalculateGrid = new Ext.grid.GridPanel({
		footer : true,
		height : 300,
		width : 600,
		anchor : '90%',
		id : 'employeePaymentGrid',
		autoScroll : true,
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : paymentCalculateStore,
					displayInfo : true,
					dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
					emptyMsg : '没有记录'
				}),
		cm : cm,
		store : paymentCalculateStore,
    	loadMask : true,
		loadMask : {
			msg : '正在加载数据...'
		}
	});

	var paymentCalculateWin = new Ext.Window({
		closable : true,
		draggable : true,
		id:'paymentCalculateWin',
		layout:'fit',
		title : '员工薪资计算',
		width : 600,
		height : 300,
		plain : true,
		items : [paymentCalculateGrid]
	});
	paymentCalculateStore.removeAll();
	paymentCalculateStore.on('beforeload', function() {
		paymentCalculateStore.baseParams = {
			queryType : "payData",
			'filter.payTypeId' : payClassId,
			'filter.monthStart' : Ext.getCmp('priod').getValue(),
			'filter.hisId' : ''
		}
	});
	paymentCalculateStore.reload();
	paymentCalculateWin.show();
}
//显示员工薪资end----------------------------

areaassignmentWin = new Ext.Window({
		closeAction : 'hide',
		closable : true,
		draggable : true,
		id:'areaassignmentWin',
		layout:'border',
		title : '审核',
		width : 700,
		height : 400,
		plain : true,
		items : [areaassignmentForm,agrid],
		buttons : [{
						text : '通过',
						id : 'companypass1',
						handler : function() {
							Ext.Ajax.request({
								url : 'workflowTaskAction.action',
								method : 'post',
								params : {
									'actionType' : 3,
									'flag' : 1,
									'filter.company_code' : Ext.getCmp('areaassignmentForm').findById('company_code').getValue(),
									'filter.flow_insid' : Ext.getCmp('areaassignmentForm').findById('flow_insid').getValue(),
									'filter.flow_id' : Ext.getCmp('areaassignmentForm').findById('flow_id').getValue(),
									'filter.step_id' : Ext.getCmp('areaassignmentForm').findById('step_id').getValue(),
									'filter.priod' : Ext.getCmp('areaassignmentForm').findById('priod').getValue(),
									'filter.flow_at' : Ext.getCmp('areaassignmentForm').findById('flow_at').getValue(),
									'filter.flow_memo' : Ext.getCmp('areaassignmentForm').findById('flow_memo').getValue(),
									'filter.flow_memos' : Ext.getCmp('areaassignmentForm').findById('flow_memos').getValue()
								},
								success : function(response, options) {
									var responseTextVale = Ext.util.JSON.decode(response.responseText)
									if(responseTextVale.isOk==0){
										Ext.Msg.alert('提示', '操作失败,失败原因:薪资已发放!');
									}else if(responseTextVale.isOk==1){
										Ext.Msg.alert('提示', '操作失败,失败原因:薪资未计算完成!');
									}else{
										//Ext.Msg.alert('提示', '操作成功!');
										areaassignmentWin.hide();
										Ext.getCmp('assignmentGrid').getStore().reload();
									}
								},
								failure : function(response, options) {
									new Ext.Viewport({layout:"fit", items:[assignmentGrid]});
								}
							});	
						}
					},{
						text : '退回',
						id : 'companyrollback1',
						handler : function() {
							var _company_code='';
							if(Ext.getCmp('areaassignmentForm').findById('this_company').getValue()==''){
								_company_code=Ext.getCmp('areaassignmentForm').findById('company_code').getValue();
							}else{
								_company_code=agrid.selModel.selections.keys;
								alert(_company_code);
							}
							Ext.Ajax.request({
								url : 'workflowTaskAction.action',
								method : 'post',
								params : {
									'actionType' : 3,
									'flag' : 2,
									'filter.rollcompany_code' : _company_code,
									'filter.company_code' : Ext.getCmp('areaassignmentForm').findById('company_code').getValue(),
									'filter.flow_insid' : Ext.getCmp('areaassignmentForm').findById('flow_insid').getValue(),
									'filter.flow_id' : Ext.getCmp('areaassignmentForm').findById('flow_id').getValue(),
									'filter.step_id' : Ext.getCmp('areaassignmentForm').findById('step_id').getValue(),
									'filter.priod' : Ext.getCmp('areaassignmentForm').findById('priod').getValue(),
									'filter.flow_at' : Ext.getCmp('areaassignmentForm').findById('flow_at').getValue(),
									'filter.flow_memo' : Ext.getCmp('areaassignmentForm').findById('flow_memo').getValue(),
									'filter.flow_memos' : Ext.getCmp('areaassignmentForm').findById('flow_memos').getValue()
								},
								success : function(response, options) {
									var responseTextVale = Ext.util.JSON.decode(response.responseText)
									if(responseTextVale.isOk==0){
										Ext.Msg.alert('提示', '操作失败,失败原因:薪资已发放!');
									}else if(responseTextVale.isOk==1){
										Ext.Msg.alert('提示', '操作失败,失败原因:薪资未计算完成!');
									}else{
										//Ext.Msg.alert('提示', '操作成功!');
										areaassignmentWin.hide();
										Ext.getCmp('assignmentGrid').getStore().reload();
									}
								},
								failure : function(response, options) {
									new Ext.Viewport({layout:"fit", items:[assignmentGrid]});
								}
							});		
						}
					}]
	});
	
//分公司结束-----------------------------

									